<HTML>
 
<!-- Mirrored from www.javapractices.com/apps/movies/javadoc/src-html/hirondelle/movies/login/LoginView.html by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:30:19 GMT -->
<HEAD>
  <TITLE>
LoginView.java
  </TITLE>
  <LINK REL ='stylesheet' TYPE='text/css' HREF='../../../../highlight.css' TITLE='Style'>
 </HEAD>
 <BODY>
<PRE>
<span class='keyword'>package</span> hirondelle.movies.login;<a name=line.1></a>
<a name=line.2></a>
<span class='keyword'>import</span> hirondelle.movies.util.ui.OnClose;<a name=line.3></a>
<span class='keyword'>import</span> hirondelle.movies.util.ui.StandardDialog;<a name=line.4></a>
<span class='keyword'>import</span> java.util.*;<a name=line.5></a>
<span class='keyword'>import</span> hirondelle.movies.util.ui.UiUtil;<a name=line.6></a>
<span class='keyword'>import</span> javax.swing.*;<a name=line.7></a>
<a name=line.8></a>
<span class='comment'>/**<a name=line.9></a>
  Show a screen asking the user for their user name and password.<a name=line.10></a>
  <a name=line.11></a>
  &lt;P&gt;This class wires its actions to call the {@link LoginController} when a button <a name=line.12></a>
   is clicked.<a name=line.13></a>
   <a name=line.14></a>
  &lt;P&gt;This implementation of a login screen can be improved. Currently, it uses <a name=line.15></a>
  {@link StandardDialog}, in order to demonstrate reuse of a standard dialog class. <a name=line.16></a>
  However, a login screen should be a {@link javax.swing.JFrame}, &lt;em&gt;not a dialog&lt;/em&gt;.<a name=line.17></a>
  This results in some issues :<a name=line.18></a>
  &lt;ul&gt;<a name=line.19></a>
   &lt;li&gt;the close button doesn't work <a name=line.20></a>
   &lt;li&gt;there's no image in the title bar<a name=line.21></a>
  &lt;/ul&gt;<a name=line.22></a>
   <a name=line.23></a>
  You can fix these issues by not using {@link StandardDialog}, and just using a &lt;tt&gt;JFrame&lt;/tt&gt;<a name=line.24></a>
  instead.<a name=line.25></a>
*/</span><a name=line.26></a>
<span class='keyword'>final</span> <span class='keyword'>class</span> LoginView {<a name=line.27></a>
<a name=line.28></a>
  <span class='comment'>/**  Constructor.  */</span><a name=line.29></a>
  LoginView(LoginController aController){<a name=line.30></a>
    fController = aController;    <a name=line.31></a>
  }<a name=line.32></a>
  <a name=line.33></a>
  <span class='comment'>/** Show the login screen. */</span><a name=line.34></a>
  <span class='keyword'>void</span> showLoginScreen(){<a name=line.35></a>
    JFrame NO_OWNER = <span class='keyword'>null</span>;<a name=line.36></a>
    fStandardDialog = <span class='keyword'>new</span> StandardDialog(<a name=line.37></a>
      NO_OWNER, <span class='literal'>"Login"</span>, <a name=line.38></a>
      <span class='keyword'>true</span>, OnClose.EXIT, getBody(), getButtons()<a name=line.39></a>
    );<a name=line.40></a>
    fStandardDialog.setDefaultButton(fLogin);<a name=line.41></a>
    fStandardDialog.display();<a name=line.42></a>
  }<a name=line.43></a>
  <a name=line.44></a>
  <span class='comment'>/** Allow the user to attempt to log in again. */</span><a name=line.45></a>
  <span class='keyword'>void</span> tryAgain(){<a name=line.46></a>
    fMessage.setText(<span class='literal'>"Failed. Please try again."</span>);<a name=line.47></a>
  }<a name=line.48></a>
  <a name=line.49></a>
  <span class='comment'>/** Return the user name entered by the user. */</span><a name=line.50></a>
  String getUserName(){<a name=line.51></a>
    <span class='keyword'>return</span> fUserName.getText();<a name=line.52></a>
  }<a name=line.53></a>
  <a name=line.54></a>
  <span class='comment'>/** Return the password entered by the user. */</span><a name=line.55></a>
  String getPassword(){<a name=line.56></a>
    <span class='keyword'>return</span> fPassword.getText();<a name=line.57></a>
  }<a name=line.58></a>
  <a name=line.59></a>
  <span class='comment'>/** Remove the login screen. */</span><a name=line.60></a>
  <span class='keyword'>void</span> close(){<a name=line.61></a>
    fStandardDialog.dispose();<a name=line.62></a>
  }<a name=line.63></a>
  <a name=line.64></a>
  <span class='comment'>// PRIVATE //<a name=line.65></a>
</span>  <span class='keyword'>private</span> LoginController fController;<a name=line.66></a>
  <span class='keyword'>private</span> StandardDialog fStandardDialog;<a name=line.67></a>
  <span class='keyword'>private</span> JTextField fUserName;  <a name=line.68></a>
  <span class='keyword'>private</span> JPasswordField fPassword;<a name=line.69></a>
  <span class='keyword'>private</span> JLabel fMessage;<a name=line.70></a>
  <span class='keyword'>private</span> JButton fLogin;<a name=line.71></a>
  <a name=line.72></a>
  <span class='keyword'>private</span> JPanel getBody(){<a name=line.73></a>
    JPanel result = <span class='keyword'>new</span> JPanel();<a name=line.74></a>
    result.setLayout(<span class='keyword'>new</span> BoxLayout(result, BoxLayout.Y_AXIS));<a name=line.75></a>
    <a name=line.76></a>
    fMessage = <span class='keyword'>new</span> JLabel(<span class='literal'>"Please log in."</span>);<a name=line.77></a>
    result.add(fMessage);<a name=line.78></a>
    result.add(Box.createVerticalStrut(<span class='literal'>5</span>));<a name=line.79></a>
    <a name=line.80></a>
    result.add(<span class='keyword'>new</span> JLabel(<span class='literal'>"User Name"</span>));<a name=line.81></a>
    fUserName = <span class='keyword'>new</span> JTextField();<a name=line.82></a>
    fUserName.setColumns(<span class='literal'>15</span>);<a name=line.83></a>
    result.add(fUserName);<a name=line.84></a>
    <a name=line.85></a>
    result.add(<span class='keyword'>new</span> JLabel(<span class='literal'>"Password"</span>));<a name=line.86></a>
    fPassword = <span class='keyword'>new</span> JPasswordField();<a name=line.87></a>
    result.add(fPassword);<a name=line.88></a>
    <a name=line.89></a>
    UiUtil.alignAllX(result, UiUtil.AlignX.LEFT);<a name=line.90></a>
    <span class='keyword'>return</span> result;<a name=line.91></a>
  }<a name=line.92></a>
<a name=line.93></a>
  <span class='keyword'>private</span> List&lt;JButton&gt; getButtons(){<a name=line.94></a>
    List&lt;JButton&gt; result = <span class='keyword'>new</span> ArrayList&lt;JButton&gt;();<a name=line.95></a>
    fLogin = <span class='keyword'>new</span> JButton(<span class='literal'>"Login"</span>);<a name=line.96></a>
    fLogin.setActionCommand(LoginController.LOGIN);<a name=line.97></a>
    fLogin.addActionListener(fController);<a name=line.98></a>
    result.add(fLogin);<a name=line.99></a>
    <a name=line.100></a>
    JButton cancel = <span class='keyword'>new</span> JButton(<span class='literal'>"Cancel"</span>);<a name=line.101></a>
    cancel.setActionCommand(LoginController.CANCEL);<a name=line.102></a>
    cancel.addActionListener(fController);<a name=line.103></a>
    result.add(cancel);<a name=line.104></a>
    <a name=line.105></a>
    <span class='keyword'>return</span> result;<a name=line.106></a>
  }<a name=line.107></a>
}<a name=line.108></a>
<a name=line.109></a>
 <a name=line.110></a>
</PRE><a name=line.111></a>
 </BODY><a name=line.112></a>
<HTML><a name=line.113></a>
